<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;

use App\Models\Usersinfo;
use App\Models\Linksrecordexport;
use App\Services\DeviceService;
use App\Services\EmailService;
use Log,Exception,DB;

class Linkrecordexport implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $links_id;
    protected $time_start;
    protected $time_end;
    protected $users_id;
    protected $excel;
    protected $row;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($links_id,$time_start,$time_end,$users_id)
    {
        $this->links_id = $links_id;
        $this->time_start = $time_start;
        $this->time_end = $time_end;
        $this->users_id = $users_id;
        $this->excel = "";
        $this->row = 1;
    }

    /**
     * 公众号内容通知
     *
     * @return void
     */
    public function handle()
    {
        $userInfo = Usersinfo::getUsersInfoDetial($this->users_id);
        $today = date("Ymd",time());
        if (empty($userInfo) || $userInfo->email == "") {
            // 添加失败记录
            Linksrecordexport::updateLinkRecordReport($this->links_id,$today,1,3);
            return "";
        }
        try {            
            $tbnum = (date("m",$this->time_start)%4);
            $config = [
                'path' => public_path('/excel/export') // xlsx文件保存路径
            ];
            $excel = new \Vtiful\Kernel\Excel($config);
            $fileObject = $excel->constMemory($this->links_id.".xlsx", NULL, false);

            $this->excel = $fileObject->header(['外链ID','渠道码','省','市','系统','设备','浏览器','IP','打开时间','小程序打开时间','二维码长按时间','点击名片时间','页面隐藏时间']);
            $starttime = time();
            DB::table("links_record_".$tbnum)->where("links_id",$this->links_id)->whereBetween("created_at",[$this->time_start,$this->time_end])->orderBy("id","ASC")->chunk(1000, function ($linkRecords) {
                foreach ($linkRecords as $linkRecord) {
                    $this->excel->insertText($this->row, 0, $linkRecord->links_id);
                    $this->excel->insertText($this->row, 1, $linkRecord->code);
                    $this->excel->insertText($this->row, 2, $linkRecord->province);
                    $this->excel->insertText($this->row, 3, $linkRecord->city);
                    $system = DeviceService::systemLists($linkRecord->system);
                    $device = DeviceService::deviceLists($linkRecord->device);
                    $browser = DeviceService::browserNames($linkRecord->browser);
                    $this->excel->insertText($this->row, 4, $system);
                    $this->excel->insertText($this->row, 5, $device);
                    $this->excel->insertText($this->row, 6, $browser);
                    $this->excel->insertText($this->row, 7, $linkRecord->ip);
                    $web_time = ($linkRecord->web_time == 1) ? "" : date("Y-m-d H:i:s",$linkRecord->web_time);
                    $this->excel->insertText($this->row, 8, $web_time);
                    $mini_open_time = ($linkRecord->mini_open_time == 1) ? "" : date("Y-m-d H:i:s",$linkRecord->mini_open_time);
                    $this->excel->insertText($this->row, 9, $mini_open_time);
                    $mini_show_time = ($linkRecord->mini_show_time == 1) ? "" : date("Y-m-d H:i:s",$linkRecord->mini_show_time);
                    $this->excel->insertText($this->row, 10, $mini_show_time);
                    $mini_press_time = ($linkRecord->mini_press_time == 1) ? "" : date("Y-m-d H:i:s",$linkRecord->mini_press_time);
                    $this->excel->insertText($this->row, 11, $mini_press_time);
                    $mini_card_time = ($linkRecord->mini_card_time == 1) ? "" : date("Y-m-d H:i:s",$linkRecord->mini_card_time);
                    $this->excel->insertText($this->row, 12, $mini_card_time);
                    $mini_hide_time = ($linkRecord->mini_hide_time == 1) ? "" : date("Y-m-d H:i:s",$linkRecord->mini_hide_time);
                    $this->excel->insertText($this->row, 13, $mini_hide_time);
                    $this->row += 1;
                }
            });
            $this->excel->output();
            $endtime = time();
            $exe_time = $endtime-$starttime;
            // 发送邮箱
            EmailService::sendLinkRecordEmail($userInfo->email,"天天外链","天天外链-访问数据表","天天外链数据导出成功,请在下方附件获取数据记录!",$this->links_id);
            // 更新发送记录
            Linksrecordexport::updateLinkRecordReport($this->links_id,$today,$exe_time,2);
            // 删除本地文件
            unlink(public_path("/excel/export".$this->links_id.".xlsx"));
        } catch (Exception $e) {
            Linksrecordexport::updateLinkRecordReport($this->links_id,$today,1,4);
        }
    }
}
